<template>
  <div class="member-order">
    <XtxTabs v-model="activeName" @tab-click="tabClick">
      <XtxTabsPannel
        v-for="item in orderStatus"
        :key="item"
        :label="item.label"
        :name="item.name"
      ></XtxTabsPannel>
    </XtxTabs>

    <!-- 订单列表 -->
    <div class="order-list">
      <!-- 数据加载中效果 -->
      <div v-if="loading" class="loading"></div>
      <!-- 暂无数据 -->
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <!-- 商品列表 -->
      <orderItem
        v-for="item in orderList"
        :key="item.id"
        :order="item"
        @on-cancel="handlerCancel"
        @on-delete="handleDelete"
        @on-confirm="hendleConfirm"
        @on-logistics="hendleLogistics"
      ></orderItem>
    </div>
    <!-- 分页 -->
    <xtxPagination
      v-if="total > 0"
      :current-page="reqParams.page"
      :pageSize="reqParams.pageSize"
      :total="total"
      @currentPage="reqParams.page = $event"
    ></xtxPagination>

    <!-- 取消订单组件 -->
    <OrderCancel ref="OrderCancelCom"></OrderCancel>

    <!-- 查看物流组件 -->
    <orderLogistics ref="orderLogistics"></orderLogistics>
  </div>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import { orderStatus } from '@/api/constants'
import orderItem from './components/order-item.vue'
import { confirmOrder, delteOrder, findOrderList } from '@/api/order'
import OrderCancel from './components/order-cancel.vue'
import Confirm from '@/components/library/Comfirm'
import Message from '@/components/library/Message'
import orderLogistics from './components/order-Logistics.vue'
export default {
  name: 'MemberOrder',
  components: { orderItem, OrderCancel, orderLogistics },
  setup (props) {
    // tab栏默认选中的选项
    const activeName = ref('all')

    // 存储返回数据的载体
    const orderList = ref([])
    // 查询参数
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      orderState: 0
    })

    // 控制loading
    const loading = ref(false)
    // 请求的数据的总条数
    const total = ref(0)
    const getOrderList = () => {
      loading.value = true
      findOrderList(reqParams).then((data) => {
        orderList.value = data.result.items
        total.value = data.result.counts
        loading.value = false
      })
    }

    // 监听reqParams的变化发请求
    watch(
      reqParams,
      () => {
        getOrderList()
      },
      { immediate: true }
    )

    // 点击选项卡  触发tab栏切换
    const tabClick = ({ index }) => {
      reqParams.page = 1
      reqParams.orderState = index
    }

    // 删除订单
    const handleDelete = (order) => {
      Confirm({ text: '是否删除？' }).then(() => {
        delteOrder(order.id).then(() => {
          Message({ text: '删除成功！' })
          getOrderList()
        })
      })
    }

    return {
      activeName,
      orderStatus,
      orderList,
      tabClick,
      loading,
      total,
      reqParams,
      handleDelete,
      ...useCancel(),
      ...useConfirm(),
      ...useLogistics()
    }
  }
}

// 取消订单
export const useCancel = () => {
  const OrderCancelCom = ref(null)
  const handlerCancel = (order) => {
    OrderCancelCom.value.open(order)
  }

  return { handlerCancel, OrderCancelCom }
}

// 确认收货
export const useConfirm = () => {
  const hendleConfirm = (order) => {
    Confirm({ text: '确认收货！' }).then(() => {
      confirmOrder(order.id).then(() => {
        Message({ text: '已收货！' })
        // 修改订单的状态
        order.orderState = 4
      })
    })
  }
  return { hendleConfirm }
}

// 查看物流  hendleLogistics
export const useLogistics = () => {
  const orderLogistics = ref(null)
  const hendleLogistics = (order) => {
    orderLogistics.value.open(order)
  }

  return { hendleLogistics, orderLogistics }
}
</script>

<style scoped lang="less">
.member-order {
  height: 100%;
  background-color: #fff;
}
.order-list {
  background: #fff;
  padding: 20px;
}
.order-list {
  padding: 20px;
  position: relative;
  min-height: 400px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, 0.9) url(../../../assets/images/loading.gif)
    no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
